package com.zz.apk.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import android.widget.ProgressBar
import android.widget.TextView
import com.zz.apk.dialog.BaseDialogFragment
import com.zz.apk.utils.FilesUtil
import com.zz.framework.R

class ProgressDialogFragment: BaseDialogFragment() {

    var percent:Int = 0
    var progress:Long = 0
    var total:Long = 0
    var progressBar:ProgressBar?=null
    var tvSize:TextView?=null
    var tvPercent:TextView?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.BottomDialog2)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        val window = dialog?.window
        window?.let {
            window.decorView.setPadding(80, 0, 80, 0)
            window.setGravity(Gravity.CENTER)
            val layoutParams = window.attributes
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            window.attributes = layoutParams
        }
        return inflater.inflate(R.layout.dialog_progress, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        progressBar = view.findViewById(R.id.progressBar)
        tvPercent = view.findViewById(R.id.tvPercent)
        tvPercent = view.findViewById(R.id.tvPercent)
        updateView()
    }

    fun update(percent:Int,progress:Long,total:Long){
        this.percent = percent
        this.progress = progress
        this.total = total
        updateView()
    }

    @SuppressLint("SetTextI18n")
    private fun updateView(){
        progressBar?.progress = percent
        tvSize?.text = "${FilesUtil.formatFileSize(progress)}/${FilesUtil.formatFileSize(total)}"
        tvPercent?.text = "$percent %"
    }
}